﻿#region The MIT License (MIT)
// The MIT License (MIT)
// Copyright (c) 2010 - 2013 Sovacode LLC
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in all copies or substantial portions
// of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#endregion

using System;
using System.IO;
using System.Text;
using Sovacode.DemoApp.Model;
using System.Collections.Generic;

/* Importing namespaces */
using Sovacode.RubiksCube.Common.ViewModel;

namespace Sovacode.DemoApp.ViewModel.BasicViewModel
{
    /// <summary>
    /// Base class for view model with link to collection of DataObjects
    /// </summary>
    public abstract class ChildsHolderViewModel : ViewModelTop
    {
        #region Private fields

        /// <summary>
        /// The data objects
        /// </summary>
        private ICollection<SimpleDataObject> _dataObjects;
        /// <summary>
        /// The selected data object
        /// </summary>
        private SimpleDataObject _selectedDataObject;

        #endregion

        #region Public properties

        /// <summary>
        /// Selected Data Object
        /// </summary>
        public SimpleDataObject SelectedDataObject
        {
            get { return _selectedDataObject; }
            set
            {
                _selectedDataObject = value;

                OnPropertyChanged(() => SelectedDataObject);

                UpdateView();
                
                Refresh();
            }
        }

        /// <summary>
        /// Gets the description file.
        /// </summary>
        /// <value>The description file.</value>
        protected virtual string DescriptionFile
        {
            get { return string.Empty; }
        }

        /// <summary>
        /// Simple Description
        /// </summary>
        public string Description
        {
            get
            {
                if (string.IsNullOrWhiteSpace(DescriptionFile))
                {
                    return "No Description";
                }

                string fileName = Path.Combine(Environment.CurrentDirectory, string.Format("Description\\{0}", DescriptionFile));

                if (File.Exists(fileName))
                {
                    return File.ReadAllText(fileName);
                }

                return "No Description";
            }
        }

        /// <summary>
        /// Collection of Data Objects
        /// </summary>
        public ICollection<SimpleDataObject> DataObjects
        {
            get { return _dataObjects; }
            set
            {
                _dataObjects = value;

                OnPropertyChanged(() => DataObjects);
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Update View after Selected DataObject change
        /// </summary>
        protected virtual void UpdateView()
        {
            
        }

        /// <summary>
        /// Shows the commands.
        /// </summary>
        /// <param name="commands">The commands.</param>
        /// <param name="message">The message.</param>
        protected void ShowInformation(IReadOnlyCollection<string> commands, string message)
        {
            if (commands == null || commands.Count == 0)
            {
                ShowError("Commands list is empty or null");

                return;
            }

            var commandsText = new StringBuilder();

            commandsText.AppendLine(message);

            foreach (var commandName in commands)
            {
                commandsText.AppendLine(commandName);
            }

            ShowMessage(commandsText.ToString());
        }
        #endregion
    }
}